package com.fenda.jetpacknavigation;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fenda.jetpacknavigation.databinding.FragmentDetailBinding;
import io.reactivex.disposables.CompositeDisposable;

public class DetailFragment extends Fragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private FragmentDetailBinding binding;

    private final CompositeDisposable mDisposable = new CompositeDisposable();

    public DetailFragment() {

    }

    public static DetailFragment newInstance(String param1, String param2) {
        DetailFragment fragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //数据DataBinding绑定
        MyViewModel myViewModel = new ViewModelProvider(getActivity(), new ViewModelProvider.NewInstanceFactory()).get(MyViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_detail, container, false);
        binding.setData(myViewModel);
        binding.setLifecycleOwner(getActivity());
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        int testNum = getArguments().getInt("testNum");
        TextView tv = getView().findViewById(R.id.textView3);
        tv.setText(String.valueOf(testNum));
        getView().findViewById(R.id.button2).setOnClickListener(
                Navigation.createNavigateOnClickListener(R.id.action_detailFragment_to_homeFragment2)
        );


        //数据库获取
//        UserDatabase userDatabase = UserDatabase.getInstance(getActivity());
//        UserDao userDao = userDatabase.getUserDao();


    }

    @Override
    public void onStop() {
        super.onStop();
        //取消所有订阅
        mDisposable.clear();
    }
}